package com.wbdgj.ui.mine;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.internal.LinkedTreeMap;
import com.igexin.sdk.PushManager;
import com.wbdgj.R;
import com.wbdgj.base.BaseActivity;
import com.wbdgj.base.BaseApplication;
import com.wbdgj.http.HttpAdapter;
import com.wbdgj.http.OnResponseListener;
import com.wbdgj.model.ResultObjectModel;
import com.wbdgj.ui.MainActivity;
import com.wbdgj.utils.SpKeyUtils;
import com.wbdgj.utils.ToastUtils;

import butterknife.BindView;
import butterknife.OnClick;
import retrofit2.Call;

public class QqPhoneActivity extends BaseActivity{

    private Context context = this;
    private String phone;

    @BindView(R.id.login_phone_et)
    EditText login_phone_et;

    @BindView(R.id.dxCode)
    EditText dxCode;
    @BindView(R.id.new_password01)
    EditText new_password01;

    private final int totalCount = 60;
    private int time = 1;

    @BindView(R.id.hq)
    TextView hq;

    private boolean isClick = false;
    private String openId;
    private String cid;
    private boolean pwdIs = false;
    private String pwdStr;
    @Override
    protected int getLayoutResId() {
        return R.layout.ui_wx_phone;
    }

    @Override
    protected void initView() {
        openId = getIntent().getStringExtra("openId");
        cid = PushManager.getInstance().getClientid(this);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            hq.setText(String.valueOf(String.valueOf(totalCount - time))+"秒后重新发送");
            hq.setTextColor(getResources().getColor(R.color.red));
            time++;
            if (time > totalCount) {
                next();
            } else {
                startCount();
            }
        }
    };

    private void startCount() {
        getWindow().getDecorView().postDelayed(runnable, 1000);
    }

    private void next() {
        isClick = false;
        hq.setText("点击获取");
        hq.setTextColor(getResources().getColor(R.color.colorPrimary));
    }

    @OnClick(value = {R.id.sure_btn, R.id.hq})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.hq:
                if (isClick){
                    ToastUtils.toastShort("请稍后再点！");
                    return;
                }
                if (TextUtils.isEmpty(login_phone_et.getText().toString())){
                    ToastUtils.toastShort("手机号不能为空！");
                    return;
                }
                userscode();
                verify();
                break;
            case R.id.sure_btn:
                if (TextUtils.isEmpty(login_phone_et.getText().toString())){
                    ToastUtils.toastShort("请输入手机号！");
                    return;
                }
                if (TextUtils.isEmpty(dxCode.getText().toString())){
                    ToastUtils.toastShort("请输入验证码！");
                    return;
                }
                if (pwdIs){
                    if (TextUtils.isEmpty(new_password01.getText().toString())){
                        ToastUtils.toastShort("请输入新密码！");
                        return;
                    }
                }
                qbind();
                break;
        }
    }

    private void userscode() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().queryAuth(login_phone_et.getText().toString());
        call.enqueue(new OnResponseListener<ResultObjectModel>(this) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                    isClick = true;
                    time = 1;
                    startCount();
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }

    private void verify() {
        Call<ResultObjectModel> call = HttpAdapter.getSerives().verify(login_phone_et.getText().toString());
        call.enqueue(new OnResponseListener<ResultObjectModel>(this) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    findViewById(R.id.pwdLay).setVisibility(View.GONE);
                    pwdIs = false;
                }else{
                    pwdIs = true;
                    findViewById(R.id.pwdLay).setVisibility(View.VISIBLE);
                }
            }
        });
    }

    private void qbind() {
        if (pwdIs){
            pwdStr = new_password01.getText().toString();
        }else{
            pwdStr = "";
        }
        Call<ResultObjectModel> call = HttpAdapter.getSerives().qbind(openId, login_phone_et.getText().toString(), dxCode.getText().toString(), pwdStr);
        call.enqueue(new OnResponseListener<ResultObjectModel>(this) {
            @Override
            protected void onSuccess(ResultObjectModel userModelBaseResultModel) {
                if(userModelBaseResultModel.getResultCode().equals("0000")){
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                    PushManager.getInstance().bindAlias(context, login_phone_et.getText().toString(), cid);
                    LinkedTreeMap linkedTreeMap = (LinkedTreeMap) userModelBaseResultModel.getData();
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.TOKEN, linkedTreeMap.get("TOKEN")+"");
                    BaseApplication.getInstance().getSpUtil().putString(SpKeyUtils.IS_AUTO_LOGIN, "1");
                    if (linkedTreeMap.containsKey("NAME")){
                        if ((linkedTreeMap.get("NAME")+"").equals("")){
                            Intent intent = new Intent(context, MainActivity.class);
                            intent.putExtra("sm", 1);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }else{
                            Intent intent = new Intent(context, MainActivity.class);
                            intent.putExtra("sm", 0);
                            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                            startActivity(intent);
                        }
                    }else{
                        Intent intent = new Intent(context, MainActivity.class);
                        intent.putExtra("sm", 1);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
                        startActivity(intent);
                    }
                }else{
                    ToastUtils.toastShort(userModelBaseResultModel.getResultMsg());
                }
            }
        });
    }
}
